package com.cal.utms.model.dealing;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.cal.utms.dao.dealing.DealDao;
import com.cal.utms.dao.dealing.InstrumentDao;
import com.cal.utms.dao.maintenance.InstrumentTypeDao;
import com.cal.utms.dao.maintenance.IssuerDaoImpl;
import com.cal.utms.dto.dealing.InsIssueReceiveDto;
import com.cal.utms.entity.Deal;
import com.cal.utms.entity.DealInstrumentMap;
import com.cal.utms.entity.Instrument;
import com.cal.utms.entity.InstrumentTransaction;
import com.cal.utms.entity.InstrumentType;
import com.cal.utms.entity.Issuer;

public class InsIssueReceiveModel {
	
	public Deal updateDIM(Deal deal, InsIssueReceiveDto insIssueReceiveDto){
		String[] recs = insIssueReceiveDto.getInsTrList().split(";");
		String[] fields = null;
		DealDao dealDao = new DealDao();
		List<DealInstrumentMap> dimList = deal.getDealInstrumentMaps();
		if(dimList==null) dimList = new ArrayList<DealInstrumentMap>();
		InstrumentTransaction insTraOne = null;
		InstrumentTransaction insTraTwo = null;
		InstrumentTransaction insTraThree = null;
		InstrumentTransaction insTraFour = null;
		
		if(deal.getDealType().getCode().equals("PURCH")){
			if(recs.length!=1){
				insIssueReceiveDto.setStatus("Purchase needs 1 instrument only");
				insIssueReceiveDto.setSuccess(false);
				return deal;
			}else{
				DealInstrumentMap dim = deal.getDealInstrumentMaps().get(0);
				
				if(dim==null){
					insIssueReceiveDto.setStatus("Error, No DIM records found");
					insIssueReceiveDto.setSuccess(false);
					return deal;
				}
				String rec = recs[0];
				fields = rec.split(",");
				if(dim.getInstrument().getInstrumentCode().equals(fields[1])){
					if(deal.getDealInstrumentMaps().get(0).getStatus().equals(fields[5])){
						insIssueReceiveDto.setStatus("No changes found");
						insIssueReceiveDto.setSuccess(false);
						return deal;
					}else if(dim.getStatus().equals("Not Received From Customer")){
						insTraOne = dim.getInstrumentTransactions().get(0);
						if(insTraOne==null){
							insIssueReceiveDto.setStatus("Error, No InsTra records found");
							insIssueReceiveDto.setSuccess(false);
							return deal;						}
						
						if(fields[5].equals("Received From Customer")){
							dim.setStatus(fields[5]);
							insTraOne.setModifiedDate(new Date());
							insTraOne.setComment("Received From Customer");
							dimList.add(dim);
							dealDao.updateDims(dimList, insIssueReceiveDto);							
							
						}else if(fields[5].equals("Dispatched to Custodian")){							 
							dim.setStatus(fields[5]);
							insTraOne.setModifiedDate(new Date());
							insTraOne.setComment("Received From Customer");
							dimList.add(dim);
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setTransAmount(insTraOne.getTransAmount());
							insTraTwo.setComment("Dispatched to Custodian");
							insTraTwo.setStatus("OUT");
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setDate(new Date());
							dim.getInstrumentTransactions().add(insTraTwo);
							dealDao.updateDims(dimList, insIssueReceiveDto);
						}else{
							insIssueReceiveDto.setStatus("Invalid Status");
							insIssueReceiveDto.setSuccess(false);
							return deal;
						}
					}else if(dim.getStatus().equals("Received From Customer")){
						insTraOne = dim.getInstrumentTransactions().get(0);
						if(insTraOne==null){
							insIssueReceiveDto.setStatus("Error, No InsTra records found");
							insIssueReceiveDto.setSuccess(false);
							return deal;						}
						
						if(fields[5].equals("Dispatched to Custodian")){							 
							dim.setStatus(fields[5]);
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setTransAmount(insTraOne.getTransAmount());
							insTraTwo.setComment("Dispatched to Custodian");
							insTraTwo.setStatus("OUT");
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setDate(new Date());
							dim.getInstrumentTransactions().add(insTraTwo);
							dealDao.updateDims(dimList, insIssueReceiveDto);
						}else{
							insIssueReceiveDto.setStatus("Invalid Status");
							insIssueReceiveDto.setSuccess(false);
							return deal;
						}
					}else{
						insIssueReceiveDto.setStatus("Data Issue!");
						insIssueReceiveDto.setSuccess(false);
						return deal;
					}
					
					
				}else{
					insIssueReceiveDto.setStatus("Instruments mismatch found");
					insIssueReceiveDto.setSuccess(false);
					return deal;
				}
			}			
			
		}else if(deal.getDealType().getCode().equals("SALE")){
			if(recs.length!=1){
				insIssueReceiveDto.setStatus("Sale needs 1 instrument only");
				insIssueReceiveDto.setSuccess(false);
				return deal;
			}else{
				DealInstrumentMap dim = deal.getDealInstrumentMaps().get(0);
				if(dim==null){
					insIssueReceiveDto.setStatus("Error, No DIM records found");
					insIssueReceiveDto.setSuccess(false);
					return deal;
				}
				String rec = recs[0];
				fields = rec.split(",");
				if(dim.getInstrument().getInstrumentCode().equals(fields[1])){
					if(deal.getDealInstrumentMaps().get(0).getStatus().equals(fields[5])){
						insIssueReceiveDto.setStatus("No changes found");
						insIssueReceiveDto.setSuccess(false);
						return deal;
					}else if(dim.getStatus().equals("Not Issued by Custodian")){
						insTraOne = dim.getInstrumentTransactions().get(0);
						if(insTraOne==null){
							insIssueReceiveDto.setStatus("Error, No InsTra records found");
							insIssueReceiveDto.setSuccess(false);
							return deal;
						}
						
						if(fields[5].equals("Issued by Custodian")){
							dim.setStatus(fields[5]);
							insTraOne.setModifiedDate(new Date());
							insTraOne.setComment("Issued by Custodian");
							dimList.add(dim);
							dealDao.updateDims(dimList, insIssueReceiveDto);							
							
						}else if(fields[5].equals("Dispatched to Customer")){							 
							dim.setStatus(fields[5]);
							insTraOne.setModifiedDate(new Date());
							insTraOne.setComment("Issued by Custodian");
							dimList.add(dim);
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setTransAmount(insTraOne.getTransAmount());
							insTraTwo.setComment("Dispatched to Customer");
							insTraTwo.setStatus("OUT");
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setDate(new Date());
							dim.getInstrumentTransactions().add(insTraTwo);
							dealDao.updateDims(dimList, insIssueReceiveDto);
						}else{
							insIssueReceiveDto.setStatus("Invalid Status");
							insIssueReceiveDto.setSuccess(false);
							return deal;
						}
					}else if(dim.getStatus().equals("Issued by Custodian")){
						insTraOne = dim.getInstrumentTransactions().get(0);
						if(insTraOne==null){
							insIssueReceiveDto.setStatus("Error, No InsTra records found");
							insIssueReceiveDto.setSuccess(false);
							return deal;						}
						
						if(fields[5].equals("Dispatched to Customer")){							 
							dim.setStatus(fields[5]);
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setTransAmount(insTraOne.getTransAmount());
							insTraTwo.setComment("Dispatched to Customer");
							insTraTwo.setStatus("OUT");
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setDate(new Date());
							dim.getInstrumentTransactions().add(insTraTwo);
							dealDao.updateDims(dimList, insIssueReceiveDto);
						}else{
							insIssueReceiveDto.setStatus("Invalid Status");
							insIssueReceiveDto.setSuccess(false);
							return deal;
						}
					}else{
						insIssueReceiveDto.setStatus("Data Issue!");
						insIssueReceiveDto.setSuccess(false);
						return deal;
					}					
				}else{
					insIssueReceiveDto.setStatus("Instruments mismatch found");
					insIssueReceiveDto.setSuccess(false);
					return deal;
				}
			}			
		}else if(deal.getDealType().getCode().equals("REPO")){
			if(recs.length<1){
				insIssueReceiveDto.setStatus("Repo needs atleast one collateral");
				insIssueReceiveDto.setSuccess(false);
				return deal;
			}else{
				DealInstrumentMap dim = null;				
				InstrumentDao insDao = new InstrumentDao();
				double insVal = 0;
				double partialAmount = 0;
				double insTranVal = 0;
				
				for(String rec : recs){
					System.out.println(rec);
					fields = rec.split(",");
					//fields[0]:InstrumentType
					//fields[1]:InstrumentCode
					//fields[2]:Value
					//fields[3]:Issuer
					//fields[4]:Maturity
					//fields[5]:Collateral Status
					//fields[6]:TempStatus
					//fields[7]:Partial Amount
					
					//instype:Test Ins code:DBTDB value:0 issuer:ARPI maturity:10/04/2016 status:Exchanged-IN To Custodian tempStatus:old partial:0
					System.out.println(" instype:"+fields[0]+" code:"+fields[1]+" value:"+fields[2]+" issuer:"+fields[3]+" maturity:"+fields[4]+" status:"+fields[5]+" tempStatus:"+fields[6]);
					
					Instrument instrument = insDao.getByCode(fields[1]);
					if(instrument==null){
						insIssueReceiveDto.setStatus("Cannot Find Instrument: "+fields[1]);
						insIssueReceiveDto.setSuccess(false);
						return deal;
					}
					
					try{
						insVal = Double.parseDouble(fields[2]);
					}catch(NumberFormatException nfe){
						insIssueReceiveDto.setStatus("Invalid Value: "+fields[2]);
						insIssueReceiveDto.setSuccess(false);
						return deal;
					}
					
					partialAmount = insVal;
					/*try{
						partialAmount = Double.parseDouble(fields[7]);
					}catch(NumberFormatException nfe){
						insIssueReceiveDto.setStatus("Invalid Partial Amount: "+fields[7]);
						insIssueReceiveDto.setSuccess(false);
						return deal;
					}*/
					if(partialAmount<0){
						insIssueReceiveDto.setStatus("Minus Partial Amount: "+fields[7]);
						insIssueReceiveDto.setSuccess(false);
						return deal;
					}else if(partialAmount>0){
						if(instrument.getInstrumentType().getScripts()==1 && instrument.getInstrumentType().getDenominals()==1){
							if(instrument.getMinDenominal()==null){
								insIssueReceiveDto.setStatus("Cannot Find Minimum Denominal for: "+instrument.getInstrumentCode());
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}else{
								if(partialAmount%instrument.getMinDenominal().doubleValue()!=0){
									insIssueReceiveDto.setStatus("Partial Amount should be a multiple of Minimum Denominal: "+instrument.getMinDenominal().doubleValue());
									insIssueReceiveDto.setSuccess(false);
									return deal;
								}
							}
						}else if(instrument.getInstrumentType().getScripts()==1 && instrument.getInstrumentType().getDenominals()==0){
							insIssueReceiveDto.setStatus("Partial Amount Not Allowed for :"+instrument.getInstrumentType().getInstrumentName());
							insIssueReceiveDto.setSuccess(false);
							return deal;
						}
					}
					
					for(DealInstrumentMap tempDim: deal.getDealInstrumentMaps()){
						if(tempDim.getInstrument().getInstrumentCode().equals(fields[1])){
							dim = tempDim;
							break;
						}
					}
					if(dim==null){
						dim = new DealInstrumentMap();
						dim.setDate(new Date());
						dim.setDeal(deal);						
						dim.setInstrument(instrument);
						dim.setInstrumentTransactions(new ArrayList<InstrumentTransaction>());
					}
					
					Collections.sort(dim.getInstrumentTransactions(), new Comparator<InstrumentTransaction>(){
						@Override
						public int compare(InstrumentTransaction insTraOne, InstrumentTransaction insTraTwo){
							return insTraOne.getDate().compareTo(insTraTwo.getDate());
						}
					});
					
					if(dim.getInstrumentTransactions().size()==1){
						insTraOne = dim.getInstrumentTransactions().get(0);
						insTraTwo = null;
						insTraThree = null;
						insTraFour = null;
					}else if(dim.getInstrumentTransactions().size()==2){
						insTraOne = dim.getInstrumentTransactions().get(0);
						System.out.println("InsTraOne:"+insTraOne.getDate());
						insTraTwo = dim.getInstrumentTransactions().get(1);
						System.out.println("InsTraTwo:"+insTraTwo.getDate());
						insTraThree = null;
						insTraFour = null;
					}else if(dim.getInstrumentTransactions().size()==3){
						insTraOne = dim.getInstrumentTransactions().get(0);
						System.out.println("InsTraOne:"+insTraOne.getDate());
						insTraTwo = dim.getInstrumentTransactions().get(1);
						System.out.println("InsTraTwo:"+insTraTwo.getDate());
						insTraThree = dim.getInstrumentTransactions().get(2);
						System.out.println("InsTraThree:"+insTraThree.getDate());
						insTraFour = null;
					}else if(dim.getInstrumentTransactions().size()==4){
						insTraOne = dim.getInstrumentTransactions().get(0);
						System.out.println("InsTraOne:"+insTraOne.getDate());
						insTraTwo = dim.getInstrumentTransactions().get(1);
						System.out.println("InsTraTwo:"+insTraTwo.getDate());
						insTraThree = dim.getInstrumentTransactions().get(2);
						System.out.println("InsTraThree:"+insTraThree.getDate());
						insTraFour = dim.getInstrumentTransactions().get(3);
						System.out.println("InsTraFour:"+insTraFour.getDate());
					}else{
						insTraOne = null;
						insTraTwo = null;
					}
					
					if(fields[5].equals("Issued by Custodian")){						
						dim.getInstrument().setLastDeal(deal);
						if(insTraOne == null){							

							BigDecimal partialAmt = new BigDecimal(partialAmount);
							insTranVal = partialAmount;
							BigDecimal balance = dim.getInstrument().getInstrumentValue().subtract(partialAmt);
							if(balance.doubleValue()<0){
								insIssueReceiveDto.setStatus("Partial Amount is too large: "+fields[7]);
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}else if(balance.doubleValue()==0){
								dim.getInstrument().setStatus("COLOUT");
								if(dim.getInstrument().getValueOut()==null){
									dim.getInstrument().setValueOut(partialAmt);
								}else{
									dim.getInstrument().setValueOut(dim.getInstrument().getValueOut().add(partialAmt));
								}								
								dim.getInstrument().setInstrumentValue(new BigDecimal(0));
							}else{
								dim.getInstrument().setInstrumentValue(balance);
								if(dim.getInstrument().getValueOut()==null){
									dim.getInstrument().setValueOut(partialAmt);
								}else{
									dim.getInstrument().setValueOut(dim.getInstrument().getValueOut().add(partialAmt));
								}
							}							
							
							insTraOne = new InstrumentTransaction();
							insTraOne.setDealInstrumentMap(dim);
							insTraOne.setDate(new Date());
							insTraOne.setComment("Issued by Custodian");
							insTraOne.setStatus("IN");
							insTraOne.setTransAmount(new BigDecimal(insTranVal));
							dim.setStatus("Issued by Custodian");
							dim.getInstrumentTransactions().add(insTraOne);
						}						
					}else if(fields[5].equals("Dispatched to Customer")){
						dim.getInstrument().setLastDeal(deal);
						if(insTraOne == null){
							
							BigDecimal partialAmt = new BigDecimal(partialAmount);
							insTranVal = partialAmount;
							BigDecimal balance = dim.getInstrument().getInstrumentValue().subtract(partialAmt);
							if(balance.doubleValue()<0){
								insIssueReceiveDto.setStatus("Partial Amount is too large: "+fields[7]);
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}else if(balance.doubleValue()==0){
								dim.getInstrument().setStatus("COLOUT");
								if(dim.getInstrument().getValueOut()==null){
									dim.getInstrument().setValueOut(partialAmt);
								}else{
									dim.getInstrument().setValueOut(dim.getInstrument().getValueOut().add(partialAmt));
								}
								dim.getInstrument().setInstrumentValue(new BigDecimal(0));
							}else{
								dim.getInstrument().setInstrumentValue(balance);
								if(dim.getInstrument().getValueOut()==null){
									dim.getInstrument().setValueOut(partialAmt);
								}else{
									dim.getInstrument().setValueOut(dim.getInstrument().getValueOut().add(partialAmt));
								}
							}				
							
							
							
							insTraOne = new InstrumentTransaction();
							insTraOne.setDealInstrumentMap(dim);
							insTraOne.setDate(new Date());
							insTraOne.setComment("Issued by Custodian");
							insTraOne.setStatus("IN");
							insTraOne.setTransAmount(new BigDecimal(insTranVal));
							dim.getInstrumentTransactions().add(insTraOne);
						}
						if(insTraTwo == null){
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setDate(new Date());
							insTraTwo.setComment("Dispatched to Customer");
							insTraTwo.setStatus("OUT");
							insTraTwo.setTransAmount(new BigDecimal(insTranVal));
							dim.getInstrumentTransactions().add(insTraTwo);
						}
						dim.setStatus("Dispatched to Customer");
						
					}else if(fields[5].equals("Not Received From Customer")){
						dim.getInstrument().setLastDeal(deal);
						if(insTraOne == null){
							
							BigDecimal partialAmt = new BigDecimal(partialAmount);
							insTranVal = partialAmount;
							BigDecimal balance = dim.getInstrument().getInstrumentValue().subtract(partialAmt);
							if(balance.doubleValue()<0){
								insIssueReceiveDto.setStatus("Partial Amount is too large: "+fields[7]);
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}else if(balance.doubleValue()==0){
								dim.getInstrument().setStatus("COLOUT");
								if(dim.getInstrument().getValueOut()==null){
									dim.getInstrument().setValueOut(partialAmt);
								}else{
									dim.getInstrument().setValueOut(dim.getInstrument().getValueOut().add(partialAmt));
								}
								dim.getInstrument().setInstrumentValue(new BigDecimal(0));
							}else{
								dim.getInstrument().setInstrumentValue(balance);
								if(dim.getInstrument().getValueOut()==null){
									dim.getInstrument().setValueOut(partialAmt);
								}else{
									dim.getInstrument().setValueOut(dim.getInstrument().getValueOut().add(partialAmt));
								}
							}						
							
							
							insTraOne = new InstrumentTransaction();
							insTraOne.setDealInstrumentMap(dim);
							insTraOne.setDate(new Date());
							insTraOne.setComment("Issued by Custodian");
							insTraOne.setStatus("IN");
							insTraOne.setTransAmount(new BigDecimal(insTranVal));
							dim.getInstrumentTransactions().add(insTraOne);
						}
						if(insTraTwo == null){
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setStatus("OUT");
							insTraTwo.setTransAmount(new BigDecimal(insTranVal));
							dim.getInstrumentTransactions().add(insTraTwo);
							insTraTwo.setDate(new Date());
						}
						insTraTwo.setModifiedDate(new Date());
						insTraTwo.setComment("Not Received From Customer");
						dim.setStatus("Not Received From Customer");						
					}else if(fields[5].equals("Received From Customer")){
						dim.getInstrument().setLastDeal(deal);
						if((new Date()).compareTo(deal.getMaturityDate())<0){
							insIssueReceiveDto.setStatus("Deal Not Matured: "+insIssueReceiveDto.getDealCode());
							insIssueReceiveDto.setSuccess(false);
							return deal;
						}
						if(insTraOne == null){
							
							BigDecimal partialAmt = new BigDecimal(partialAmount);
							insTranVal = partialAmount;
							BigDecimal balance = dim.getInstrument().getInstrumentValue().subtract(partialAmt);
							if(balance.doubleValue()<0){
								insIssueReceiveDto.setStatus("Partial Amount is too large: "+fields[7]);
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}else if(balance.doubleValue()==0){
								dim.getInstrument().setStatus("COLOUT");
								if(dim.getInstrument().getValueOut()==null){
									dim.getInstrument().setValueOut(partialAmt);
								}else{
									dim.getInstrument().setValueOut(dim.getInstrument().getValueOut().add(partialAmt));
								}
								dim.getInstrument().setInstrumentValue(new BigDecimal(0));
							}else{
								dim.getInstrument().setInstrumentValue(balance);
								if(dim.getInstrument().getValueOut()==null){
									dim.getInstrument().setValueOut(partialAmt);
								}else{
									dim.getInstrument().setValueOut(dim.getInstrument().getValueOut().add(partialAmt));
								}
							}							
							
							
							insTraOne = new InstrumentTransaction();
							insTraOne.setDealInstrumentMap(dim);
							insTraOne.setDate(new Date());
							insTraOne.setComment("Issued by Custodian");
							insTraOne.setStatus("IN");
							insTraOne.setTransAmount(new BigDecimal(insTranVal));
							dim.getInstrumentTransactions().add(insTraOne);
						}
						if(insTraTwo == null){
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setStatus("OUT");
							insTraTwo.setTransAmount(new BigDecimal(insTranVal));
							dim.getInstrumentTransactions().add(insTraTwo);
							insTraTwo.setDate(new Date());
							insTraTwo.setComment("Not Received From Customer");
						}else if(insTraTwo.getComment()!=null && insTraTwo.getComment().equals("Dispatched to Customer")){
							insTraTwo.setComment("Not Received From Customer");
							insTraTwo.setModifiedDate(new Date());
						}
						if(insTraThree == null){
							
							BigDecimal partialAmt = new BigDecimal(partialAmount);
							insTranVal = partialAmount;
							BigDecimal instVal = dim.getInstrument().getInstrumentValue().add(partialAmt);
							if(dim.getInstrument().getValueOut()==null){
								insIssueReceiveDto.setStatus("Cannot Find ValueOut for: "+dim.getInstrument().getInstrumentCode());
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}
							BigDecimal valOut = dim.getInstrument().getValueOut().subtract(partialAmt);
							if(valOut.doubleValue()<0){
								insIssueReceiveDto.setStatus("Minus ValueOut for: "+dim.getInstrument().getInstrumentCode()+" from Deal"+deal.getDealCode());
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}else if(instVal.doubleValue()>0){
								dim.getInstrument().setStatus("IN");
								dim.getInstrument().setValueOut(valOut);
								dim.getInstrument().setInstrumentValue(instVal);
							}else{
								dim.getInstrument().setInstrumentValue(instVal);
								dim.getInstrument().setValueOut(valOut);
							}							
							
							insTraThree = new InstrumentTransaction();
							insTraThree.setDealInstrumentMap(dim);
							insTraThree.setStatus("IN");
							insTraThree.setTransAmount(new BigDecimal(insTranVal));
							dim.getInstrumentTransactions().add(insTraThree);
							insTraThree.setDate(new Date());
							insTraThree.setComment("Received From Customer");
						}						
						dim.setStatus("Received From Customer");						
					}else if(fields[5].equals("Exchanged-IN From Customer")){     //For Exchange Scenario
						//dim.getInstrument().setStatus("IN");
						dim.getInstrument().setLastDeal(deal);
						if(insTraOne == null){
							
							BigDecimal partialAmt = new BigDecimal(partialAmount);
							insTranVal = partialAmount;
							BigDecimal balance = dim.getInstrument().getInstrumentValue().subtract(partialAmt);
							if(balance.doubleValue()<0){
								insIssueReceiveDto.setStatus("Partial Amount is too large: "+fields[7]);
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}else if(balance.doubleValue()==0){
								dim.getInstrument().setStatus("COLOUT");
								if(dim.getInstrument().getValueOut()==null){
									dim.getInstrument().setValueOut(partialAmt);
								}else{
									dim.getInstrument().setValueOut(dim.getInstrument().getValueOut().add(partialAmt));
								}
								dim.getInstrument().setInstrumentValue(new BigDecimal(0));
							}else{
								dim.getInstrument().setInstrumentValue(balance);
								if(dim.getInstrument().getValueOut()==null){
									dim.getInstrument().setValueOut(partialAmt);
								}else{
									dim.getInstrument().setValueOut(dim.getInstrument().getValueOut().add(partialAmt));
								}
							}							
							
							insTraOne = new InstrumentTransaction();
							insTraOne.setDealInstrumentMap(dim);
							insTraOne.setDate(new Date());
							insTraOne.setComment("Issued by Custodian");
							insTraOne.setStatus("IN");
							dim.getInstrumentTransactions().add(insTraOne);
						}
						insTraOne.setTransAmount(new BigDecimal(insTranVal));
						if(insTraTwo == null){
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setStatus("OUT");
							dim.getInstrumentTransactions().add(insTraTwo);
							insTraTwo.setDate(new Date());
							insTraTwo.setComment("Dispatched to Customer");
						}
						insTraTwo.setTransAmount(new BigDecimal(insTranVal));
						if(insTraThree == null){
							BigDecimal partialAmt = new BigDecimal(partialAmount);
							insTranVal = partialAmount;
							BigDecimal instVal = dim.getInstrument().getInstrumentValue().add(partialAmt);
							if(dim.getInstrument().getValueOut()==null){
								insIssueReceiveDto.setStatus("Cannot Find ValueOut for: "+dim.getInstrument().getInstrumentCode());
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}
							BigDecimal valOut = dim.getInstrument().getValueOut().subtract(partialAmt);
							if(valOut.doubleValue()<0){
								insIssueReceiveDto.setStatus("Minus ValueOut for: "+dim.getInstrument().getInstrumentCode()+" from Deal"+deal.getDealCode());
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}else if(instVal.doubleValue()>0){
								dim.getInstrument().setStatus("IN");
								dim.getInstrument().setValueOut(valOut);
								dim.getInstrument().setInstrumentValue(instVal);
							}else{
								dim.getInstrument().setInstrumentValue(instVal);
								dim.getInstrument().setValueOut(valOut);
							}							
							
							insTraThree = new InstrumentTransaction();
							insTraThree.setDealInstrumentMap(dim);
							insTraThree.setStatus("IN");
							dim.getInstrumentTransactions().add(insTraThree);
							insTraThree.setDate(new Date());
							insTraThree.setComment("Exchanged-IN From Customer");
						}
						insTraThree.setTransAmount(new BigDecimal(0));						
						dim.setStatus("Exchanged-IN From Customer");						
					}else if(fields[5].equals("Dispatched to Custodian")){
						//dim.getInstrument().setStatus("IN");
						dim.getInstrument().setLastDeal(deal);
						if((new Date()).compareTo(deal.getMaturityDate())<0){
							insIssueReceiveDto.setStatus("Deal Not Matured: "+insIssueReceiveDto.getDealCode());
							insIssueReceiveDto.setSuccess(false);
							return deal;
						}
						if(insTraOne == null){
							
							BigDecimal partialAmt = new BigDecimal(partialAmount);
							insTranVal = partialAmount;
							BigDecimal balance = dim.getInstrument().getInstrumentValue().subtract(partialAmt);
							if(balance.doubleValue()<0){
								insIssueReceiveDto.setStatus("Partial Amount is too large: "+fields[7]);
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}else if(balance.doubleValue()==0){
								dim.getInstrument().setStatus("COLOUT");
								if(dim.getInstrument().getValueOut()==null){
									dim.getInstrument().setValueOut(partialAmt);
								}else{
									dim.getInstrument().setValueOut(dim.getInstrument().getValueOut().add(partialAmt));
								}
								dim.getInstrument().setInstrumentValue(new BigDecimal(0));
							}else{
								dim.getInstrument().setInstrumentValue(balance);
								if(dim.getInstrument().getValueOut()==null){
									dim.getInstrument().setValueOut(partialAmt);
								}else{
									dim.getInstrument().setValueOut(dim.getInstrument().getValueOut().add(partialAmt));
								}
							}
							
							
							insTraOne = new InstrumentTransaction();
							insTraOne.setDealInstrumentMap(dim);
							insTraOne.setDate(new Date());
							insTraOne.setComment("Issued by Custodian");
							insTraOne.setStatus("IN");
							insTraOne.setTransAmount(new BigDecimal(insTranVal));
							dim.getInstrumentTransactions().add(insTraOne);
						}
						if(insTraTwo == null){
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setStatus("OUT");
							insTraTwo.setTransAmount(new BigDecimal(insTranVal));
							dim.getInstrumentTransactions().add(insTraTwo);
							insTraTwo.setDate(new Date());
							insTraTwo.setComment("Not Received From Customer");
						}else if(insTraTwo.getComment()!=null && insTraTwo.getComment().equals("Dispatched to Customer")){
							insTraTwo.setComment("Not Received From Customer");
							insTraTwo.setModifiedDate(new Date());
						}
						if(insTraThree == null){
							
							BigDecimal partialAmt = new BigDecimal(partialAmount);
							insTranVal = partialAmount;
							BigDecimal instVal = dim.getInstrument().getInstrumentValue().add(partialAmt);
							if(dim.getInstrument().getValueOut()==null){
								insIssueReceiveDto.setStatus("Cannot Find ValueOut for: "+dim.getInstrument().getInstrumentCode());
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}
							BigDecimal valOut = dim.getInstrument().getValueOut().subtract(partialAmt);
							if(valOut.doubleValue()<0){
								insIssueReceiveDto.setStatus("Minus ValueOut for: "+dim.getInstrument().getInstrumentCode()+" from Deal"+deal.getDealCode());
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}else if(instVal.doubleValue()>0){
								dim.getInstrument().setStatus("IN");
								dim.getInstrument().setValueOut(valOut);
								dim.getInstrument().setInstrumentValue(instVal);
							}else{
								dim.getInstrument().setInstrumentValue(instVal);
								dim.getInstrument().setValueOut(valOut);
							}
							
							
							insTraThree = new InstrumentTransaction();
							insTraThree.setDealInstrumentMap(dim);
							insTraThree.setStatus("IN");
							insTraThree.setTransAmount(new BigDecimal(insTranVal));
							dim.getInstrumentTransactions().add(insTraThree);
							insTraThree.setDate(new Date());
							insTraThree.setComment("Received From Customer");
						}
						if(insTraFour == null){
							insTraFour = new InstrumentTransaction();
							insTraFour.setDealInstrumentMap(dim);
							insTraFour.setStatus("OUT");
							insTraFour.setTransAmount(new BigDecimal(insTranVal));
							dim.getInstrumentTransactions().add(insTraFour);
							insTraFour.setDate(new Date());
							insTraFour.setComment("Dispatched to Custodian");			
						}
						dim.setStatus("Dispatched to Custodian");						
					}else if(fields[5].equals("Exchanged-IN To Custodian")){
						//dim.getInstrument().setStatus("IN");
						dim.getInstrument().setLastDeal(deal);
						/*if((new Date()).compareTo(deal.getMaturityDate())<0){
							insIssueReceiveDto.setStatus("Deal Not Matured: "+insIssueReceiveDto.getDealCode());
							insIssueReceiveDto.setSuccess(false);
							return deal;
						}*/
						if(insTraOne == null){
							
							BigDecimal partialAmt = new BigDecimal(partialAmount);
							insTranVal = partialAmount;
							BigDecimal balance = dim.getInstrument().getInstrumentValue().subtract(partialAmt);
							if(balance.doubleValue()<0){
								insIssueReceiveDto.setStatus("Partial Amount is too large: "+fields[7]);
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}else if(balance.doubleValue()==0){
								dim.getInstrument().setStatus("COLOUT");
								if(dim.getInstrument().getValueOut()==null){
									dim.getInstrument().setValueOut(partialAmt);
								}else{
									dim.getInstrument().setValueOut(dim.getInstrument().getValueOut().add(partialAmt));
								}
								dim.getInstrument().setInstrumentValue(new BigDecimal(0));
							}else{
								dim.getInstrument().setInstrumentValue(balance);
								if(dim.getInstrument().getValueOut()==null){
									dim.getInstrument().setValueOut(partialAmt);
								}else{
									dim.getInstrument().setValueOut(dim.getInstrument().getValueOut().add(partialAmt));
								}
							}							
							
							insTraOne = new InstrumentTransaction();
							insTraOne.setDealInstrumentMap(dim);
							insTraOne.setDate(new Date());
							insTraOne.setComment("Issued by Custodian");
							insTraOne.setStatus("IN");
							dim.getInstrumentTransactions().add(insTraOne);
						}
						insTraOne.setTransAmount(new BigDecimal(insVal));
						if(insTraTwo == null){
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setStatus("OUT");
							dim.getInstrumentTransactions().add(insTraTwo);
							insTraTwo.setDate(new Date());
							insTraTwo.setComment("Dispatched to Customer");
						}
						insTraTwo.setTransAmount(new BigDecimal(insVal));
						if(insTraThree == null){
							
							BigDecimal partialAmt = new BigDecimal(partialAmount);
							insTranVal = partialAmount;
							BigDecimal instVal = dim.getInstrument().getInstrumentValue().add(partialAmt);
							if(dim.getInstrument().getValueOut()==null){
								insIssueReceiveDto.setStatus("Cannot Find ValueOut for: "+dim.getInstrument().getInstrumentCode());
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}
							BigDecimal valOut = dim.getInstrument().getValueOut().subtract(partialAmt);
							if(valOut.doubleValue()<0){
								insIssueReceiveDto.setStatus("Minus ValueOut for: "+dim.getInstrument().getInstrumentCode()+" from Deal"+deal.getDealCode());
								insIssueReceiveDto.setSuccess(false);
								return deal;
							}else if(instVal.doubleValue()>0){
								dim.getInstrument().setStatus("IN");
								dim.getInstrument().setValueOut(valOut);
								dim.getInstrument().setInstrumentValue(instVal);
							}else{
								dim.getInstrument().setInstrumentValue(instVal);
								dim.getInstrument().setValueOut(valOut);
							}
							
							insTraThree = new InstrumentTransaction();
							insTraThree.setDealInstrumentMap(dim);
							insTraThree.setStatus("IN");
							dim.getInstrumentTransactions().add(insTraThree);
							insTraThree.setDate(new Date());
							insTraThree.setComment("Exchanged-IN From Customer");
						}
						insTraThree.setTransAmount(new BigDecimal(0));
						if(insTraFour == null){
							insTraFour = new InstrumentTransaction();
							insTraFour.setDealInstrumentMap(dim);
							insTraFour.setStatus("OUT");
							dim.getInstrumentTransactions().add(insTraFour);
							insTraFour.setDate(new Date());
							insTraFour.setComment("Exchanged-IN To Custodian");			
						}
						insTraFour.setTransAmount(new BigDecimal(0));
						dim.setStatus("Exchanged-IN To Custodian");						
					}		
					
					else{
						insIssueReceiveDto.setStatus("Invalid Status for REPO: "+fields[5]);
						insIssueReceiveDto.setSuccess(false);
						return deal;
					}					
					
					dimList.add(dim);
					instrument = null;
					dim = null;
					insTraOne = null;
					insTraTwo = null;
					insTraThree = null;
					insTraFour = null;
				}				
				dealDao.updateDims(dimList, insIssueReceiveDto);				
			}			
			
		}else if(deal.getDealType().getCode().equals("RREPO")){
			if(recs.length<1){
				insIssueReceiveDto.setStatus("RevRepo needs atleast one collateral");
				insIssueReceiveDto.setSuccess(false);
				return deal;
			}else{
				DealInstrumentMap dim = null;				
				InstrumentDao insDao = new InstrumentDao();
				double insVal = 0;
				
				for(String rec : recs){
					System.out.println(rec);
					fields = rec.split(",");
					System.out.println(" instype:"+fields[0]+" code:"+fields[1]+" value:"+fields[2]+" issuer:"+fields[3]+" maturity:"+fields[4]+" status:"+fields[5]+" tempStatus:"+fields[6]);
					
					
					try{
						insVal = Double.parseDouble(fields[2]);
					}catch(NumberFormatException nfe){
						insIssueReceiveDto.setStatus("Cannot Parse Instrument Value: "+fields[2]);
						insIssueReceiveDto.setSuccess(false);
						return deal;
					}
					Instrument instrument = insDao.getByCode(fields[1]);
					if(instrument==null){
						instrument = new Instrument();
						InstrumentType insType = new InstrumentTypeDao().getInstrumentTypeByName(fields[0]);
						if(insType == null){
							insIssueReceiveDto.setStatus("Invalid Instrument Type: "+fields[0]);
							insIssueReceiveDto.setSuccess(false);
							return deal;
						}
						instrument.setInstrumentType(insType);
						instrument.setInstrumentCode(fields[1]);
						instrument.setInstrumentValue(new BigDecimal(insVal));
						Issuer issuer = new IssuerDaoImpl().getIssuerByIssuerCode(fields[3]);
						if(issuer == null){
							insIssueReceiveDto.setStatus("Cannot Find Issuer: "+fields[3]);
							insIssueReceiveDto.setSuccess(false);
							return deal;
						}
						instrument.setIssuer(issuer);
						DateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
						Date matDt = null;
						try{
							matDt = df.parse(fields[4]);
						}catch(ParseException pe){
							insIssueReceiveDto.setStatus("Cannot Parse Maturity Date: "+fields[4]);
							insIssueReceiveDto.setSuccess(false);
							return deal;
						}
						instrument.setMaturityDate(matDt);
						instrument.setIssueDate(new Date());						
					}else{
						if(!instrument.getInstrumentType().getInstrumentName().equals(fields[0])){
							insIssueReceiveDto.setStatus("Instrument Name already exiats under Intrument Type: "+instrument.getInstrumentType().getInstrumentName());
							insIssueReceiveDto.setSuccess(false);
							return deal;
						}
					}
					
					for(DealInstrumentMap tempDim: deal.getDealInstrumentMaps()){
						if(tempDim.getInstrument().getInstrumentCode().equals(fields[1])){
							dim = tempDim;
							break;
						}
					}
					if(dim==null){
						dim = new DealInstrumentMap();
						dim.setDate(new Date());
						dim.setDeal(deal);						
						dim.setInstrument(instrument);
						dim.setInstrumentTransactions(new ArrayList<InstrumentTransaction>());
					}
					
					Collections.sort(dim.getInstrumentTransactions(), new Comparator<InstrumentTransaction>(){
						@Override
						public int compare(InstrumentTransaction insTraOne, InstrumentTransaction insTraTwo){
							return insTraOne.getDate().compareTo(insTraTwo.getDate());
						}
					});
					
					if(dim.getInstrumentTransactions().size()==1){
						insTraOne = dim.getInstrumentTransactions().get(0);
						insTraTwo = null;
						insTraThree = null;
						insTraFour = null;
					}else if(dim.getInstrumentTransactions().size()==2){
						insTraOne = dim.getInstrumentTransactions().get(0);
						System.out.println("InsTraOne:"+insTraOne.getDate());
						insTraTwo = dim.getInstrumentTransactions().get(1);
						System.out.println("InsTraTwo:"+insTraTwo.getDate());
						insTraThree = null;
						insTraFour = null;
					}else if(dim.getInstrumentTransactions().size()==3){
						insTraOne = dim.getInstrumentTransactions().get(0);
						System.out.println("InsTraOne:"+insTraOne.getDate());
						insTraTwo = dim.getInstrumentTransactions().get(1);
						System.out.println("InsTraTwo:"+insTraTwo.getDate());
						insTraThree = dim.getInstrumentTransactions().get(2);
						System.out.println("InsTraThree:"+insTraThree.getDate());
						insTraFour = null;
					}else if(dim.getInstrumentTransactions().size()==2){
						insTraOne = dim.getInstrumentTransactions().get(0);
						System.out.println("InsTraOne:"+insTraOne.getDate());
						insTraTwo = dim.getInstrumentTransactions().get(1);
						System.out.println("InsTraTwo:"+insTraTwo.getDate());
						insTraThree = dim.getInstrumentTransactions().get(2);
						System.out.println("InsTraThree:"+insTraThree.getDate());
						insTraFour = dim.getInstrumentTransactions().get(3);
						System.out.println("InsTraFour:"+insTraFour.getDate());
					}else{
						insTraOne = null;
						insTraTwo = null;
					}
					
					if(fields[5].equals("Received From Customer")){
						dim.getInstrument().setStatus("COLIN");
						dim.getInstrument().setLastDeal(deal);
						if(insTraOne == null){
							insTraOne = new InstrumentTransaction();
							insTraOne.setDealInstrumentMap(dim);
							insTraOne.setDate(new Date());
							insTraOne.setComment("Received From Customer");
							insTraOne.setStatus("IN");
							insTraOne.setTransAmount(new BigDecimal(insVal));
							dim.setStatus("Received From Customer");
							dim.getInstrumentTransactions().add(insTraOne);
						}						
					}else if(fields[5].equals("Dispatched to Custodian")){
						dim.getInstrument().setStatus("COLIN");
						dim.getInstrument().setLastDeal(deal);
						if(insTraOne == null){
							insTraOne = new InstrumentTransaction();
							insTraOne.setDealInstrumentMap(dim);
							insTraOne.setDate(new Date());
							insTraOne.setComment("Received From Customer");
							insTraOne.setStatus("IN");
							insTraOne.setTransAmount(new BigDecimal(insVal));
							dim.getInstrumentTransactions().add(insTraOne);
						}
						if(insTraTwo == null){
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setDate(new Date());
							insTraTwo.setComment("Dispatched to Custodian");
							insTraTwo.setStatus("OUT");
							insTraTwo.setTransAmount(new BigDecimal(insVal));
							dim.getInstrumentTransactions().add(insTraTwo);
						}
						dim.setStatus("Dispatched to Custodian");
						
					}else if(fields[5].equals("Not Issued by Custodian")){
						dim.getInstrument().setStatus("COLIN");
						dim.getInstrument().setLastDeal(deal);
						if(insTraOne == null){
							insTraOne = new InstrumentTransaction();
							insTraOne.setDealInstrumentMap(dim);
							insTraOne.setDate(new Date());
							insTraOne.setComment("Received From Customer");
							insTraOne.setStatus("IN");
							insTraOne.setTransAmount(new BigDecimal(insVal));
							dim.getInstrumentTransactions().add(insTraOne);
						}
						if(insTraTwo == null){
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setStatus("OUT");
							insTraTwo.setTransAmount(new BigDecimal(insVal));
							dim.getInstrumentTransactions().add(insTraTwo);
							insTraTwo.setDate(new Date());
						}
						insTraTwo.setModifiedDate(new Date());
						insTraTwo.setComment("Not Issued by Custodian");
						dim.setStatus("Not Issued by Custodian");						
					}else if(fields[5].equals("Issued by Custodian")){
						dim.getInstrument().setStatus("COLIN");
						dim.getInstrument().setLastDeal(deal);
						if((new Date()).compareTo(deal.getMaturityDate())<0){
							insIssueReceiveDto.setStatus("Deal Not Matured: "+insIssueReceiveDto.getDealCode());
							insIssueReceiveDto.setSuccess(false);
							return deal;
						}
						if(insTraOne == null){
							insTraOne = new InstrumentTransaction();
							insTraOne.setDealInstrumentMap(dim);
							insTraOne.setDate(new Date());
							insTraOne.setComment("Received From Customer");
							insTraOne.setStatus("IN");
							insTraOne.setTransAmount(new BigDecimal(insVal));
							dim.getInstrumentTransactions().add(insTraOne);
						}
						if(insTraTwo == null){
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setStatus("OUT");
							insTraTwo.setTransAmount(new BigDecimal(insVal));
							dim.getInstrumentTransactions().add(insTraTwo);
							insTraTwo.setDate(new Date());
							insTraTwo.setComment("Not Issued by Custodian");
						}else if(insTraTwo.getComment()!=null && insTraTwo.getComment().equals("Dispatched to Custodian")){
							insTraTwo.setComment("Not Issued by Custodian");
							insTraTwo.setModifiedDate(new Date());
						}
						if(insTraThree == null){
							insTraThree = new InstrumentTransaction();
							insTraThree.setDealInstrumentMap(dim);
							insTraThree.setStatus("IN");
							insTraThree.setTransAmount(new BigDecimal(insVal));
							dim.getInstrumentTransactions().add(insTraThree);
							insTraThree.setDate(new Date());
							insTraThree.setComment("Issued by Custodian");
						}						
						dim.setStatus("Issued by Custodian");						
					}else if(fields[5].equals("Exchanged-Out From Custodian")){   //Exchange Scenario
						dim.getInstrument().setStatus("COLIN");
						dim.getInstrument().setLastDeal(deal);

						if(insTraOne == null){
							insTraOne = new InstrumentTransaction();
							insTraOne.setDealInstrumentMap(dim);
							insTraOne.setDate(new Date());
							insTraOne.setComment("Received From Customer");
							insTraOne.setStatus("IN");
							dim.getInstrumentTransactions().add(insTraOne);
						}
						insTraOne.setTransAmount(new BigDecimal(insVal));
						if(insTraTwo == null){
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setStatus("OUT");
							dim.getInstrumentTransactions().add(insTraTwo);
							insTraTwo.setDate(new Date());
							insTraTwo.setComment("Dispatched to Custodian");
						}
						insTraTwo.setTransAmount(new BigDecimal(insVal));
						if(insTraThree == null){
							insTraThree = new InstrumentTransaction();
							insTraThree.setDealInstrumentMap(dim);
							insTraThree.setStatus("IN");
							dim.getInstrumentTransactions().add(insTraThree);
							insTraThree.setDate(new Date());
							insTraThree.setComment("Exchanged-Out From Custodian");
						}
						insTraThree.setTransAmount(new BigDecimal(insVal));
						dim.setStatus("Exchanged-Out From Custodian");
					}					
					else if(fields[5].equals("Dispatched to Customer")){
						dim.getInstrument().setStatus("FREE");
						dim.getInstrument().setLastDeal(deal);
						if((new Date()).compareTo(deal.getMaturityDate())<0){
							insIssueReceiveDto.setStatus("Deal Not Matured: "+insIssueReceiveDto.getDealCode());
							insIssueReceiveDto.setSuccess(false);
							return deal;
						}
						if(insTraOne == null){
							insTraOne = new InstrumentTransaction();
							insTraOne.setDealInstrumentMap(dim);
							insTraOne.setDate(new Date());
							insTraOne.setComment("Received From Customer");
							insTraOne.setStatus("IN");
							insTraOne.setTransAmount(new BigDecimal(insVal));
							dim.getInstrumentTransactions().add(insTraOne);
						}
						if(insTraTwo == null){
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setStatus("OUT");
							insTraTwo.setTransAmount(new BigDecimal(insVal));
							dim.getInstrumentTransactions().add(insTraTwo);
							insTraTwo.setDate(new Date());
							insTraTwo.setComment("Not Issued by Custodian");
						}else if(insTraTwo.getComment()!=null && insTraTwo.getComment().equals("Dispatched to Custodian")){
							insTraTwo.setComment("Not Issued by Custodian");
							insTraTwo.setModifiedDate(new Date());
						}
						if(insTraThree == null){
							insTraThree = new InstrumentTransaction();
							insTraThree.setDealInstrumentMap(dim);
							insTraThree.setStatus("IN");
							insTraThree.setTransAmount(new BigDecimal(insVal));
							dim.getInstrumentTransactions().add(insTraThree);
							insTraThree.setDate(new Date());
							insTraThree.setComment("Issued by Custodian");
						}
						if(insTraFour == null){
							insTraFour = new InstrumentTransaction();
							insTraFour.setDealInstrumentMap(dim);
							insTraFour.setStatus("OUT");
							insTraFour.setTransAmount(new BigDecimal(insVal));
							dim.getInstrumentTransactions().add(insTraFour);
							insTraFour.setDate(new Date());
							insTraFour.setComment("Dispatched to Customer");			
						}
						dim.setStatus("Dispatched to Customer");						
					}
					else if(fields[5].equals("Exchanged-Out To Customer")){
						dim.getInstrument().setStatus("FREE");
						dim.getInstrument().setLastDeal(deal);
						if(insTraOne == null){
							insTraOne = new InstrumentTransaction();
							insTraOne.setDealInstrumentMap(dim);
							insTraOne.setDate(new Date());
							insTraOne.setComment("Received From Customer");
							insTraOne.setStatus("IN");
							dim.getInstrumentTransactions().add(insTraOne);
						}
						insTraOne.setTransAmount(new BigDecimal(insVal));
						if(insTraTwo == null){
							insTraTwo = new InstrumentTransaction();
							insTraTwo.setDealInstrumentMap(dim);
							insTraTwo.setStatus("OUT");
							dim.getInstrumentTransactions().add(insTraTwo);
							insTraTwo.setDate(new Date());
							insTraTwo.setComment("Dispatched to Custodian");
						}
						insTraTwo.setTransAmount(new BigDecimal(insVal));
						if(insTraThree == null){
							insTraThree = new InstrumentTransaction();
							insTraThree.setDealInstrumentMap(dim);
							insTraThree.setStatus("IN");
							dim.getInstrumentTransactions().add(insTraThree);
							insTraThree.setDate(new Date());
							insTraThree.setComment("Exchanged-Out From Custodian");
						}
						insTraThree.setTransAmount(new BigDecimal(insVal));
						if(insTraFour == null){
							insTraFour = new InstrumentTransaction();
							insTraFour.setDealInstrumentMap(dim);
							insTraFour.setStatus("OUT");
							dim.getInstrumentTransactions().add(insTraFour);
							insTraFour.setDate(new Date());
							insTraFour.setComment("Exchanged-Out To Customer");			
						}
						insTraFour.setTransAmount(new BigDecimal(insVal));
						dim.setStatus("Exchanged-Out To Customer");						
					}					
					else{
						insIssueReceiveDto.setStatus("Invalid Status for REPO: "+fields[5]);
						insIssueReceiveDto.setSuccess(false);
						return deal;
					}					
					//Dispatched to Custodian
					
					dimList.add(dim);
					instrument = null;
					dim = null;
					insTraOne = null;
					insTraTwo = null;
					insTraThree = null;
					insTraFour = null;
				}				
				dealDao.updateDims(dimList, insIssueReceiveDto);				
			}			
		}

		System.out.println(insIssueReceiveDto.getInsTrList());
		for(String rec : recs){
			System.out.println(rec);
			fields = rec.split(",");
			System.out.println(" instype:"+fields[0]+" code:"+fields[1]+" value:"+fields[2]+" issuer:"+fields[3]+" maturity:"+fields[4]+" status:"+fields[5]+" tempStatus:"+fields[6]);
			
		}
		return deal;
	}

}
